package com.zboot.zbootadmin.project.gen.service.impl;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.zboot.zbootadmin.common.config.druid.DruidProperties;
import com.zboot.zbootadmin.project.gen.service.GenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: zyt
 * @Description:
 * @Date: Created in 15:20 2020/7/18
 */
@Service
public class GenServiceImpl implements GenService {

    @Autowired
    private DruidProperties druidProperties;

    @Override
    public GlobalConfig globalConfig() {
        //全局配置
        GlobalConfig gc = new GlobalConfig();
        String oPath = System.getProperty("user.dir");//得到当前项目的路径
        gc.setOutputDir(oPath+"\\generateCode");   //生成文件输出根目录
        gc.setOpen(false);//生成完成后不弹出文件框
        gc.setFileOverride(true);  //文件覆盖
        gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setAuthor("zyt");// 作者
        // 自定义文件命名，注意 %s 会自动填充表实体属性！
        gc.setControllerName("%sController");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        return gc;
    }

    @Override
    public DataSourceConfig dataSourceConfig() {
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName(druidProperties.getDriverClassName());
        dsc.setUsername(druidProperties.getUsername());
        dsc.setPassword(druidProperties.getPassword());
        dsc.setUrl(druidProperties.getUrl());  //指定数据库
        return dsc;
    }

    @Override
    public StrategyConfig strategyConfig(String ...tables) {
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);      // 表名生成策略
        strategy.setInclude(tables);
        strategy.setSuperServiceClass(null);
        strategy.setSuperServiceImplClass(null);
        strategy.setSuperMapperClass(null);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        return strategy;
    }

    @Override
    public PackageConfig packageConfig(Class clazz) {
        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(clazz.getPackage().getName().substring(0,clazz.getPackage().getName().lastIndexOf(".")));
        pc.setController("controller");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setMapper("mappers");
        pc.setEntity("entity");
        pc.setXml("xml");
        return pc;
    }

    @Override
    public TemplateConfig templateConfig() {
        TemplateConfig tc = new TemplateConfig();
        tc.setController("vm/controller.java");
        tc.setService("vm/service.java");
        tc.setServiceImpl("vm/serviceImpl.java");
        tc.setEntity("vm/entity.java");
        tc.setMapper("vm/mapper.java");
        tc.setXml("vm/mapper.xml");
        return tc;
    }

    @Override
    public AutoGenerator setConfig(Class clazz, String... tables) {
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setGlobalConfig(this.globalConfig());
        autoGenerator.setDataSource(this.dataSourceConfig());
        autoGenerator.setStrategy(this.strategyConfig(tables));
        autoGenerator.setPackageInfo(this.packageConfig(clazz));
        autoGenerator.setTemplate(this.templateConfig());
        return autoGenerator;
    }

    @Override
    public void execute(Class clazz) {
        this.setConfig(clazz).execute();
    }
}
